﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LogManager.Base;
using LogManager.Base.Models.Projections;
using Raven.Client.Indexes;
using Lucene.Net.Documents;

namespace LogManager.LocalStorage.Indexes
{
    public class Events_PerPath : AbstractIndexCreationTask<Event, EventsPerPathProjection>
    {
        public Events_PerPath()
        {
            Map = events =>
                  from e in events
                  where e.Properties["RequestUrl"] != null
                  select new { LogName = e.LogName, Path = e.Properties["RequestUrl"], Count = 1, LastRequest = e.TimeStamp };
            Reduce = results =>
                  from r in results
                  group r by new { LogName = r.LogName, Path = r.Path } into g
                  select new { LogName = g.Key.LogName, Path = g.Key.Path, Count = g.Sum(x => x.Count), LastRequest = g.OrderBy(x => x.LastRequest).SingleOrDefault().LastRequest };
        }
    }
}
